import datastructure.linklist.ListNode;

/**
 * @Author ZhangCuirong
 * @Date 2025/8/4 14:00
 * @description:
 */
public class Solution92 {
    public ListNode reverseBetween(ListNode head, int left, int right) {
        ListNode pre = head;
        int i = 0;
        for (; i < left - 1; i++) {
            pre = pre.next;
        }
        ListNode cur = pre.next;
        ListNode next =null;
        for (; i < right - left - 1; i++) {
            next = cur.next;
            cur.next.next = cur;
            cur= next;
        }
        pre.next.next=next;
        pre.next= cur;
        return head;
    }

    public static void main(String[] args) {
        int[] nums = {1, 2, 3, 4, 5};
        ListNode head = ListNode.arrayToList(nums);
        Solution92 solution = new Solution92();
        ListNode newHead = solution.reverseBetween(head, 2, 4);
        ListNode.printListWithCycleCheck(newHead);
    }
}
